package com.xiaomi.front.controller;

import com.xiaomi.front.service.address.AddressService;
import com.xiaomi.front.service.bill.BillService;
import com.xiaomi.front.service.shopp_cart.ShoppCartService;
import com.xiaomi.pojo.*;
import com.xiaomi.util.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private ShoppCartService shoppCartService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private BillService billService;

    //结算
    @PostMapping("/CreateOrder")
    public String CreateOrder(int[] carid, double jiage, Model model, HttpSession session) {

        User loginuser = (User) session.getAttribute("user");
        if (loginuser == null) {
            return "front/index";
        } else {
            String name = loginuser.getUname();
            model.addAttribute("name", name);
            //可用地址显示
            List<Address> addressList = addressService.addList(loginuser.getUid());
            model.addAttribute("addlist", addressList);
            //获取选择得商品
            List<Shoppingcart> shoppingcarts = shoppCartService.getCarts(carid);
            model.addAttribute("shoppcarts", shoppingcarts);

            model.addAttribute("jiage", jiage);

            return "front/bill/orderjs";
        }
    }

    //生成订单详情
    //跳转支付页
    @PostMapping("/Order")
    public String Order(int[] commId, int addressId, double param, Model model, HttpSession session) {

        //取session中 user 得id
        User loginuser = (User) session.getAttribute("user");
        //生成订单编号
        String ordernum= ToolsUtil.orderNum();
        Boolean result = billService.CreateOrder(commId, param, addressId, loginuser.getUid(),ordernum);

        System.out.println(result);
        //根据编号查详情
        Bill bill=billService.getBillInfo(ordernum);
        model.addAttribute("bill",bill);

        //根据编号查 商品名称
        List<Commoditys> commname=billService.GetCommName(ordernum);
        model.addAttribute("commname",commname);
        return "front/bill/confirm";
    }

    //支付功能
    @RequestMapping("/PayOrder")
    public String PayOrder(int id,String orderNum,Model model){
        if(id>3||id<0){
            model.addAttribute("state","支付错误!");
        }else{
            int ifPay=billService.updateBill(id,orderNum);
            if(ifPay==1){
                model.addAttribute("state","支付成功!");
            }else{
                model.addAttribute("state","支付失败!");
            }
        }
        return "front/pay/ifpay";
    }

    //未支付点击进入支付页
    @RequestMapping("/ToOrder/{bid}")
    public String ToOrder(@PathVariable("bid") String ordernum, Model model, HttpSession session){

        //取session中 user 得id
        User loginuser = (User) session.getAttribute("user");
        if(loginuser==null){
            return "forward:/front_login.jsp";
        }else{
            //根据编号查详情
            Bill bill=billService.getBillInfo(ordernum);
            model.addAttribute("bill",bill);

            //根据编号查 商品名称
            List<Commoditys> commname=billService.GetCommName(ordernum);
            model.addAttribute("commname",commname);
            return "front/bill/confirm";
        }
    }

    //进入订单详情页
    @RequestMapping("/ToOrderInfo")
    public String ToOrderInfo(@RequestParam("bid") String ordernum, Model model, HttpSession session){

        //取session中 user 得id
        User loginuser = (User) session.getAttribute("user");
        if(loginuser==null){
            return "forward:/front_login.jsp";
        }else{
            //根据编号查详情
            Bill bill=billService.getOrderNum(loginuser.getUid(),ordernum);
            model.addAttribute("bill",bill);

            return "front/Center/OrderInfo";
        }
    }

    //ajax ---取消订单
    @RequestMapping("/ajax_cancellation_order")
    @ResponseBody
    public int Cancellation_Order(String bid,HttpSession session){
        //取session中 user 得id
        User loginuser = (User) session.getAttribute("user");
        if(loginuser==null){
            return 0;
        }else{
            int result=billService.UpdateBill(loginuser.getUid(),bid,0);
            return result;
        }
    }

    //确认 订单完成
    @RequestMapping("/CompleteOreder")
    @ResponseBody
    public int CompleteOreder(String bid,HttpSession session){

        User loginUser=(User) session.getAttribute("user");
        if(loginUser==null){
            return 0;
        }else{
            int result=billService.UpdateBill(loginUser.getUid(),bid,4);
            return result;
        }
    }
}
